<?php
    include("../service/bannerService.php");
    include("../service/imageService.php");
    
    $imagePath = "../img/banner/";
    $bannerImg = $_FILES["bannerImg"];
    $banID = $_POST["banID"];
    $proID = $_POST["products"] == "" ? "NULL" : $_POST["products"];
    $oldBannerPath = getBannerPathBy($banID);
    
    if(!empty($bannerImg['name'])){
        deletePhysicalImage($oldBannerPath[0]);
        $actionResult = updateBanner($banID, $proID, $imagePath.$bannerImg['name']);
        $result = move_uploaded_file($bannerImg["tmp_name"], $imagePath.$bannerImg['name']);
        
        if(($result > 0) && ($actionResult > 0))
            echo "<script>window.location = '../admin-page/bannerDetail.php?actionResult=ok'</script>";
        else
            echo "<script>window.location = '../admin-page/bannerDetail.php?actionResult=no'</script>";
    } else {
        $actionResult = updateBanner($banID, $proID, $oldBannerPath[0]);
        
        if($actionResult)
            echo "<script>window.location = '../admin-page/bannerDetail.php?actionResult=ok'</script>";
        else 
            echo "<script>window.location = '../admin-page/bannerDetail.php?actionResult=no'</script>";
    }
    
?>
